package ar.com.sectorwine.controllers;

import java.util.Locale;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Valid;
import javax.validation.Validator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import ar.com.sectorwine.dto.UsuarioRegistroDTO;
import ar.com.sectorwine.dto.UsuarioRegistroDTOMapper;
import ar.com.sectorwine.entities.Users;
import ar.com.sectorwine.services.UsuarioService;

/**
 * Handles requests for the application home page.
 */
@Controller
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class RegistroController {

	@Autowired
	private UsuarioService usuarioService;

	@Autowired
	private Validator validator;

	@Autowired
	private UsuarioRegistroDTOMapper usuariosDTOMapper;

	private static final Logger logger = LoggerFactory
			.getLogger(RegistroController.class);

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "registro", method = RequestMethod.GET)
	public String registro(Locale locale, Model model) {
		logger.info("Welcome home! the client locale is " + locale.toString());
		model.addAttribute("usuario", new UsuarioRegistroDTO());
		return "usuarios/registro";
	}

	@RequestMapping(value = "/registro", method = RequestMethod.POST)
	public ModelAndView create(
			@Valid @ModelAttribute("usuario") UsuarioRegistroDTO usuarioDTO,
			BindingResult result, SessionStatus status) {

		Set<ConstraintViolation<UsuarioRegistroDTO>> failures = validator
				.validate(usuarioDTO);

		if (failures.size() == 0) {
			logger.info("controller");
			Users usuario = usuariosDTOMapper
					.obtenerObjetoDelDominioDesdeDTO(usuarioDTO);

			if (usuario != null) {
				usuarioService.crearUsuario(usuario);
				status.setComplete();
				ModelAndView mav = new ModelAndView("home");
				mav.addObject("usuario", usuarioDTO);
				return mav;
			} else {
				ModelAndView mav = new ModelAndView("usuarios/registro");
				mav.addObject("usuario", usuarioDTO);
				return mav;
			}
		} else {
			ModelAndView mav = new ModelAndView("usuarios/registro");
			mav.addObject("usuario", usuarioDTO);
			for (Object o : failures) {
				logger.info(o.toString());
			}
			return mav;
		}
	}

	// registro_confirmacion
	@RequestMapping(value = "registro_confirmacion", method = RequestMethod.GET)
	public String registro_confirmacion(Locale locale, Model model,
			@RequestParam String userName,
			@RequestParam String codigoConfirmacion) {
		usuarioService.confirmarRegistro(userName, codigoConfirmacion);
		logger.info("Registracion Confirmada...");
		return "/index";
	}

}
